<template>
  <el-dialog
    :title="!dataForm.orderId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="订单编号" prop="orderNo">
      <el-input v-model="dataForm.orderNo" placeholder="订单编号"></el-input>
    </el-form-item>
    <el-form-item label="用户id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="总金额" prop="sumPrice">
      <el-input v-model="dataForm.sumPrice" placeholder="总金额"></el-input>
    </el-form-item>
    <el-form-item label="实际金额" prop="realPrice">
      <el-input v-model="dataForm.realPrice" placeholder="实际金额"></el-input>
    </el-form-item>
    <el-form-item label="下单时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="下单时间"></el-input>
    </el-form-item>
    <el-form-item label="订单状态" prop="orderState">
      <el-input v-model="dataForm.orderState" placeholder="订单状态"></el-input>
    </el-form-item>
    <el-form-item label="支付状态" prop="payState">
      <el-input v-model="dataForm.payState" placeholder="支付状态"></el-input>
    </el-form-item>
    <el-form-item label="发货状态" prop="sendState">
      <el-input v-model="dataForm.sendState" placeholder="发货状态"></el-input>
    </el-form-item>
    <el-form-item label="收货状态" prop="gatherState">
      <el-input v-model="dataForm.gatherState" placeholder="收货状态"></el-input>
    </el-form-item>
    <el-form-item label="商品id" prop="productId">
      <el-input v-model="dataForm.productId" placeholder="商品id"></el-input>
    </el-form-item>
    <el-form-item label="物流id" prop="logisticsId">
      <el-input v-model="dataForm.logisticsId" placeholder="物流id"></el-input>
    </el-form-item>
    <el-form-item label="退款id" prop="refundId">
      <el-input v-model="dataForm.refundId" placeholder="退款id"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          orderId: 0,
          orderNo: '',
          userId: '',
          sumPrice: '',
          realPrice: '',
          createTime: '',
          orderState: '',
          payState: '',
          sendState: '',
          gatherState: '',
          productId: '',
          logisticsId: '',
          refundId: ''
        },
        dataRule: {
          orderNo: [
            { required: true, message: '订单编号不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          sumPrice: [
            { required: true, message: '总金额不能为空', trigger: 'blur' }
          ],
          realPrice: [
            { required: true, message: '实际金额不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '下单时间不能为空', trigger: 'blur' }
          ],
          orderState: [
            { required: true, message: '订单状态不能为空', trigger: 'blur' }
          ],
          payState: [
            { required: true, message: '支付状态不能为空', trigger: 'blur' }
          ],
          sendState: [
            { required: true, message: '发货状态不能为空', trigger: 'blur' }
          ],
          gatherState: [
            { required: true, message: '收货状态不能为空', trigger: 'blur' }
          ],
          productId: [
            { required: true, message: '商品id不能为空', trigger: 'blur' }
          ],
          logisticsId: [
            { required: true, message: '物流id不能为空', trigger: 'blur' }
          ],
          refundId: [
            { required: true, message: '退款id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.orderId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.orderId) {
            this.$http({
              url: this.$http.adornUrl(`/goods/order/info/${this.dataForm.orderId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.orderNo = data.order.orderNo
                this.dataForm.userId = data.order.userId
                this.dataForm.sumPrice = data.order.sumPrice
                this.dataForm.realPrice = data.order.realPrice
                this.dataForm.createTime = data.order.createTime
                this.dataForm.orderState = data.order.orderState
                this.dataForm.payState = data.order.payState
                this.dataForm.sendState = data.order.sendState
                this.dataForm.gatherState = data.order.gatherState
                this.dataForm.productId = data.order.productId
                this.dataForm.logisticsId = data.order.logisticsId
                this.dataForm.refundId = data.order.refundId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/goods/order/${!this.dataForm.orderId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'orderId': this.dataForm.orderId || undefined,
                'orderNo': this.dataForm.orderNo,
                'userId': this.dataForm.userId,
                'sumPrice': this.dataForm.sumPrice,
                'realPrice': this.dataForm.realPrice,
                'createTime': this.dataForm.createTime,
                'orderState': this.dataForm.orderState,
                'payState': this.dataForm.payState,
                'sendState': this.dataForm.sendState,
                'gatherState': this.dataForm.gatherState,
                'productId': this.dataForm.productId,
                'logisticsId': this.dataForm.logisticsId,
                'refundId': this.dataForm.refundId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
